package com.zbkj.crmeb.merchant.admin.controller;

import javax.servlet.http.HttpServletRequest;

import com.common.CommonPage;
import com.common.PageParamRequest;
import com.zbkj.crmeb.store.service.EmployeeFileService;
import com.zbkj.crmeb.system.request.SystemAdminRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.common.CheckAdminToken;
import com.crmeb.core.common.CommonResult;
import com.constants.Constants;
import com.crmeb.core.utils.RedisUtil;
import com.zbkj.crmeb.system.model.SystemAdmin;
import com.zbkj.crmeb.system.request.SystemAdminAddRequest;
import com.zbkj.crmeb.system.response.SystemAdminResponse;
import com.zbkj.crmeb.system.service.SystemAdminService;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;


/**
 * 后台管理员表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/admin/mer/system/admin")
@Api(tags = "后台用户服务")

public class MerSystemAdminController extends AdminBaseController {
    @Autowired
    private SystemAdminService systemAdminService;
    @Autowired
    private CheckAdminToken checkAdminToken;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    private EmployeeFileService employeeFileService;

    /**
     * 新增后台管理员表
     * @param systemAdminAddRequest 新增参数
     * @author Mr.Zhang
     * @since 2020-04-13
     */
    @ApiOperation(value = "新增(商户)")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<SystemAdminResponse> merSave(@ModelAttribute SystemAdminAddRequest systemAdminAddRequest, HttpServletRequest request) throws Exception {
        // 设置默认商户号
        String token = checkAdminToken.getTokenFormRequest(request);
        Object merId = redisUtil.get(StrUtil.format(Constants.ADMIN_TOKEN_MER_KEY, token));
        systemAdminAddRequest.setMerId(merId.toString());
        
        SystemAdmin systemAdmin = new SystemAdmin();
        BeanUtils.copyProperties(systemAdminAddRequest, systemAdmin);
        SystemAdminResponse systemAdminResponse = systemAdminService.saveAdmin(systemAdminAddRequest);
        if(null != systemAdminResponse){
            return CommonResult.success(systemAdminResponse,"添加管理员成功");
        }else{
            return CommonResult.failed("添加管理员失败");
        }
    }

    /**
     * 功能描述: 添加员工账号
     * @Param: [systemAdminAddRequest, request]
     * @Return: com.crmeb.core.common.CommonResult<java.lang.Boolean>
     * @Author: wangzh
     * @Date: 2022/11/24 18:07
     */
    @ApiOperation("添加账号")
    @RequestMapping(value = "/saveEmployee", method = RequestMethod.POST)
    public CommonResult<Boolean> saveEmployeeAccount(@RequestBody SystemAdminAddRequest systemAdminAddRequest, HttpServletRequest request) throws Exception {
        systemAdminAddRequest.setMerId(Arrays.asList(getMerId(request)).toString())
                .setIdentityType(2);
        if (employeeFileService.saveEmployeeAccount(systemAdminAddRequest)){
            return CommonResult.success(true);
        } else {
            return CommonResult.failed(String.valueOf(false));
        }
    }

    /**
     * 功能描述: 根据传入的信息，修改员工账号
     * @Param: [systemAdminRequest]
     * @Return: com.crmeb.core.common.CommonResult<java.lang.Boolean>
     * @Author: wangzh
     * @Date: 2022/11/24 18:08
     */
    @ApiOperation("修改账号信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<Boolean> updateEmployeeAccount(@RequestBody SystemAdminRequest systemAdminRequest) throws Exception {
        if (employeeFileService.updateEmployeeAccount(systemAdminRequest)){
            return CommonResult.success(true);
        } else {
            return CommonResult.failed(String.valueOf(false));
        }
    }

    /**
     * 功能描述: 查询员工档案列表 / 传入工号，姓名，角色id模糊查询
     * @Param: [systemAdminRequest, pageParamRequest, request]
     * @Return: com.crmeb.core.common.CommonResult<com.common.CommonPage<com.zbkj.crmeb.system.response.SystemAdminResponse>>
     * @Author: wangzh
     * @Date: 2022/11/24 18:08
     */
    @ApiOperation("查询员工档案列表 / 传入工号，姓名，角色id模糊查询")
    @RequestMapping(method = RequestMethod.GET)
    public CommonResult<CommonPage<SystemAdminResponse>> getEmployeeAccountByCode(SystemAdminRequest systemAdminRequest, PageParamRequest pageParamRequest, HttpServletRequest request){
        systemAdminRequest.setMerId(Arrays.asList(getMerId(request)).toString());
        return CommonResult.success(CommonPage.restPage(employeeFileService.getEmployeeAccountByCode(systemAdminRequest, pageParamRequest)));
    }

    /**
     * 功能描述: 返回主账号名称，创建子账号时使用
     * @Param: [request]
     * @Return: com.crmeb.core.common.CommonResult<java.lang.String>
     * @Author: wangzh
     * @Date: 2022/11/25 10:23
     */
    @ApiOperation("查询主账号名称")
    @RequestMapping(value = "/primaryAccountName", method = RequestMethod.GET)
    public CommonResult<String> getPrimaryAccountName(HttpServletRequest request){
        return CommonResult.success(employeeFileService.getPrimaryAccountName(Arrays.asList(getMerId(request)).toString()), "操作成功");
    }

    /**
     * 功能描述: 根据传入的账号id删除账号
     * @Param: [id]
     * @Return: com.crmeb.core.common.CommonResult<java.lang.Boolean>
     * @Author: wangzh
     * @Date: 2022/11/25 10:24
     */
    @ApiOperation("删除账号")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public CommonResult<Boolean> deleteEmployAccount(@PathVariable Integer id){
        return CommonResult.success(employeeFileService.deleteEmployAccount(id));
    }

    /**
     * 功能描述: 传入id获取账号信息
     * @Param: [id]
     * @Return: com.crmeb.core.common.CommonResult<com.zbkj.crmeb.system.response.SystemAdminResponse>
     * @Author: wangzh
     * @Date: 2022/11/29 10:19
     */
    @ApiOperation("根据id查询账号信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CommonResult<SystemAdminResponse> getEmployAccountById(@PathVariable Integer id) throws Exception {
        return CommonResult.success(employeeFileService.getEmployAccountById(id));
    }
}



